package br.ufrgs.inf.engsoft.stp.exchange;

import br.ufrgs.inf.engsoft.stp.product.Product;
import br.ufrgs.inf.engsoft.stp.product.ProductDao;

public class ExchangeService {
	
	private ExchangeDao exchangeDao;
	private ProductDao productDao;
	
	public ExchangeService() {
		productDao = new ProductDao();
		exchangeDao = new ExchangeDao();
	}
	
	public Exchange create(Product p1, Product p2) {
		Exchange e = new Exchange(p1.getId(), p2.getId());
    	exchangeDao.create(e);
    	p1.setExchangeId(e.getId());
    	p1.setEnable(Product.NOT_ENABLE);
    	p2.setExchangeId(e.getId());
    	p2.setEnable(Product.NOT_ENABLE);
    	productDao.update(p1);
    	productDao.update(p2);
    	return e;
	}
	
	public void complete(int status, Product p1, Product p2) {
	    Exchange e = exchangeDao.read(p1.getExchangeId());
	    e.setStatus(status);
	    exchangeDao.update(e);
	    switch (status) {
    	    case Exchange.COMPLETED : 
    	        break;
    	    case Exchange.REJECTED :
    	        p1.setEnable(Product.ENABLE);
    	        p2.setEnable(Product.ENABLE);
    	        p1.setExchangeId(null);
    	        p2.setExchangeId(null);
    	        productDao.update(p1);
    	        productDao.update(p2);
	    }
	}
}
